package com.ruoyi.project.wms.service.impl;

import java.util.List;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.project.wms.factory.AsyncLogInfo;
import com.ruoyi.project.wms.mapper.WmsGoodsStockMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.project.wms.mapper.WmsOrderMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物品订单Service业务层处理
 *
 * @author zx
 * @date 2023-05-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class WmsOrderServiceImpl extends BaseController {

    private final WmsOrderMapper wmsOrderMapper;

    private final WmsGoodsStockMapper wmsGoodsStockMapper;

    /**
     * 查询物品订单
     *
     * @param id 物品订单主键
     * @return pd
     */

    public PageData selectWmsOrderById(Long id) {
        return wmsOrderMapper.selectWmsOrderById(id);
    }

    /**
     * 查询物品订单列表
     *
     * @param pd
     * @return 物品订单
     */

    public List<PageData> selectWmsOrderList(PageData pd) {
        return wmsOrderMapper.selectWmsOrderList(pd);
    }

    /**
     * 新增物品订单
     *
     * @param pd 物品订单
     * @return 结果
     */
    public int insertWmsOrder(PageData pd) {
        pd.put("create_by", SecurityUtils.getUserId());
        return wmsOrderMapper.insertWmsOrder(pd);
    }

    /**
     * 修改物品订单
     *
     * @param pd 物品订单
     * @return 结果
     */
    public int updateWmsOrder(PageData pd) {
        return wmsOrderMapper.updateWmsOrder(pd);
    }


    public int deleteWmsOrderByIds(Long[] ids) {
        return wmsOrderMapper.deleteWmsOrderByIds(ids);
    }


    public int updateOrderStatus(Long id) {
        List<PageData> pds = wmsOrderMapper.queryListOrder(id);
        pds.forEach(a -> {
            if (wmsOrderMapper.getLineNum(a) > 0) {
                PageData pageData = wmsGoodsStockMapper.selectWmsGoodsStockInfo(a);
                if (pageData == null) {
                    return;
                }
                pageData.put("type", a.get("type"));
                pageData.put("measure_unit", a.get("measure_unit"));
                pageData.put("quantity", a.get("quantity"));
                pageData.put("order_id", id);
                a.put("update_by", SecurityUtils.getUserId());
                wmsGoodsStockMapper.updateWmsGoodsStockNum(a);
                AsyncManager.me().execute(AsyncLogInfo.recordLog(pageData));
            }
            if (wmsOrderMapper.getLineNum(a) == 0 && a.get("type").equals("in")) {
                a.put("create_by", SecurityUtils.getUserId());
                a.put("update_by", SecurityUtils.getUserId());
                a.put("quantity_before",0);
                a.put("quantity_after",a.get("quantity"));
                wmsGoodsStockMapper.insertWmsGoodsStock(a);
                AsyncManager.me().execute(AsyncLogInfo.recordLog(a));
            }
        });
        return wmsOrderMapper.updateWmsOrderStatus(id);
    }
}
